local assets =
{
    Asset("ANIM", "anim/bboy_nether_blossom.zip"),
}

local prefabs =
{
    "bboy_nether_petal",
    "planted_bboy_nether_blossom",
}

local names = { "f1", "f2", "f3", "f4" }

local HEALTH_DRAIN_INTERVAL = 25 -- 每隔 25 秒执行一次
local HEALTH_DRAIN_RADIUS = 3    -- 影响半径（可按需调整）

local function DrainNearbyPlayers(inst)
    if not inst:IsValid() then return end
    local x, y, z = inst.Transform:GetWorldPosition()
    local players = FindPlayersInRange(x, y, z, HEALTH_DRAIN_RADIUS, true)
    for _, p in ipairs(players) do
        if p.components ~= nil and p.components.health ~= nil and not p.components.health:IsDead() then
            -- 第三个参数作为伤害来源标记，方便战斗日志/死亡提示
            p.components.health:DoDelta(-1, false, inst.prefab)
        end
    end
end

local function setflowertype(inst, name)
    if inst.animname == nil or (name ~= nil and inst.animname ~= name) then
        inst.animname = name or names[math.random(#names)]

        inst.AnimState:PlayAnimation(inst.animname)
    end
end

local function onsave(inst, data)
    data.anim = inst.animname
    data.planted = inst.planted
end

local function onload(inst, data)
    setflowertype(inst, data ~= nil and data.anim or nil)
    inst.planted = data ~= nil and data.planted or nil
end

local function onpickedfn(inst, picker)
    local pos = inst:GetPosition()

    if picker ~= nil then
        if picker.components.sanity ~= nil and not picker:HasTag("plantkin") then
            picker.components.sanity:DoDelta(TUNING.SANITY_TINY)
        end
    end

    TheWorld:PushEvent("plantkilled", { doer = picker, pos = pos }) -- 其他地方也推送了这个事件
end

local function CheckForPlanted(inst)
    if not inst.planted then
        AddToRegrowthManager(inst)
    end
end

--------------------------------------------------------------------------

local function commonfn(isplanted)
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    inst.AnimState:SetBank("bboy_nether_blossom")
    inst.AnimState:SetBuild("bboy_nether_blossom")
    inst.AnimState:SetRayTestOnBB(true)

    inst:SetDeploySmartRadius(DEPLOYSPACING_RADIUS[DEPLOYSPACING.LESS] / 2)

    inst:AddTag("flower")
    inst:AddTag("cattoy")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    -- 升san光环
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = TUNING.SANITYAURA_TINY

    inst:AddComponent("pickable")
    inst.components.pickable.picksound = "dontstarve/wilson/pickup_plants"
    inst.components.pickable:SetUp("bboy_nether_petal", 10)
    inst.components.pickable.onpickedfn = onpickedfn
    inst.components.pickable.remove_when_picked = true
    inst.components.pickable.quickpick = true
    inst.components.pickable.wildfirestarter = true

    MakeSmallBurnable(inst)
    if not isplanted then -- 这在加载时将为真，但在运行时间将为假并减少。
        inst:DoTaskInTime(0, CheckForPlanted)
    end
    MakeSmallPropagator(inst)

    MakeHauntableWork(inst)

    -- 每 25 秒对范围内玩家造成 1 点生命值损失
    if inst._healthdrain_task ~= nil then
        inst._healthdrain_task:Cancel()
    end
    inst._healthdrain_task = inst:DoPeriodicTask(HEALTH_DRAIN_INTERVAL, DrainNearbyPlayers)

    if not POPULATING then
        setflowertype(inst)
    end

    inst.OnSave = onsave
    inst.OnLoad = onload

    inst:ListenForEvent("onremove", function()
        if inst._healthdrain_task ~= nil then
            inst._healthdrain_task:Cancel()
            inst._healthdrain_task = nil
        end
    end)

    return inst
end

local function plainfn()
    -- NOTES(JBK): 这是为了阻止 TheSim 出现在 commonfn 回调中。
    return commonfn()
end

local function plantedflowerfn()
    local inst = commonfn(true)

    inst:SetPrefabName("bboy_nether_blossom")

    if not TheWorld.ismastersim then
        return inst
    end

    inst.planted = true

    return inst
end

return Prefab("bboy_nether_blossom", plainfn, assets, prefabs),
    Prefab("planted_bboy_nether_blossom", plantedflowerfn, assets, prefabs)
